<!--生产制造->需求计划->需求建议-->
<template>
    <div class="content-wrapper a-base-container">
        <div class="requisition-set-wrapper mrp-result-box">
            <!-- 按钮 -->
            <div class="btn-wrapper clearfix">
                <a-row class="text-center clearfix">
                    <a-col :xs="24" :sm="24" :md="24" :lg="16" :xl="12">
                        <a-col :xs="btnGrid.xs" :sm="btnGrid.sm" :md="btnGrid.md" :lg="btnGrid.lg" :xl="btnGrid.xl" v-if="activeKey == '1'">
                            <a-dropdown class="btns" key="sheng" style="display:block">
                                <a class="ant-dropdown-link" @click="e => e.preventDefault()">
                                    <a-icon class="btns-icon" type="snippets" />
                                    <div class="btns-text">生单
                                        <a-icon type="down" />
                                    </div>
                                </a>
                                <a-menu slot="overlay">
                                    <a-menu-item key="0">
                                        <a href="javascript:void(0)" @click="generateBills(1)">生成请购单</a>
                                    </a-menu-item>
                                    <a-menu-item key="1">
                                        <a href="javascript:void(0)" @click="generateBills(2)">生成采购订单</a>
                                    </a-menu-item>
                                </a-menu>
                            </a-dropdown>
                        </a-col>

                        <a-col :xs="btnGrid.xs" :sm="btnGrid.sm" :md="btnGrid.md" :lg="btnGrid.lg" :xl="btnGrid.xl" v-if="activeKey == '2'">
                            <a-dropdown class="btns" key="aaaa" style="display:block">
                                <a class="ant-dropdown-link" @click="e => e.preventDefault()">
                                    <a-icon class="btns-icon" type="snippets" />
                                    <div class="btns-text">生单
                                        <a-icon type="down" />
                                    </div>
                                </a>
                                <a-menu slot="overlay">
                                    <a-menu-item key="0">
                                        <a href="javascript:void(0)" @click="generateProduction(1)">生产需求计划</a>
                                    </a-menu-item>
                                </a-menu>
                            </a-dropdown>
                        </a-col>

                        <!-- <a-col :xs="2" :sm="2" :md="2" :lg="2" :xl="2" :class="[scType?'btns-disable':'']">
                            <div class="btns" style="display:block;margin-left:10px">
                                <a class="ant-dropdown-link">
                                    <a-icon class="btns-icon" type="delete" />
                                    <div class="btns-text">删除</div>
                                </a>
                            </div>
                        </a-col> -->
                        <!-- <a-col :xs="2" :sm="2" :md="2" :lg="2" :xl="2">
                            <div class="btns" style="display:block;margin-left:10px">
                                <a class="ant-dropdown-link" @click="e => e.preventDefault()">
                                    <a-icon class="btns-icon" type="export" />
                                    <div class="btns-text">导出</div>
                                </a>
                            </div>
                        </a-col> -->

                        <!-- <a-col :xs="2" :sm="2" :md="2" :lg="2" :xl="2">
                            <div class="btns" style="display:block;margin-left:10px">
                                <a class="ant-dropdown-link">
                                    <a-icon class="btns-icon" type="sync" />
                                    <div class="btns-text">刷新</div>
                                </a>
                            </div>
                        </a-col> -->

                    </a-col>
                </a-row>

            </div>
            <a-tabs :activeKey="activeKey" @change="callback">
                <a-tab-pane key="1" tab="采购建议">
                    <!--按钮-->
                    <!-- <div class="btn-wrapper clearfix">
                        <a-row class="text-center clearfix">
                            <a-col :xs="24" :sm="24" :md="24" :lg="16" :xl="12">
                                <a-col :xs="btnGrid.xs" :sm="btnGrid.sm" :md="btnGrid.md" :lg="btnGrid.lg" :xl="btnGrid.xl">
                                    <a-dropdown class="btns" key="sheng" style="display:block">
                                        <a class="ant-dropdown-link" @click="e => e.preventDefault()">
                                            <a-icon class="btns-icon" type="snippets" />
                                            <div class="btns-text">生单
                                                <a-icon type="down" />
                                            </div>
                                        </a>
                                        <a-menu slot="overlay">
                                            <a-menu-item key="0">
                                                <a href="javascript:void(0)" @click="generateBills(1)">生成请购单</a>
                                            </a-menu-item>
                                            <a-menu-item key="1">
                                                <a href="javascript:void(0)" @click="generateBills(2)">生成采购订单</a>
                                            </a-menu-item>
                                        </a-menu>
                                    </a-dropdown>
                                </a-col>
                            </a-col>
                        </a-row>
                    </div> -->
                    <!--内容-->
                    <section class="work-plan-content advice-warpper">
                        <a-table :columns="columns" :data-source="procurementData" :pagination="false" bordered :row-selection="rowSelectionCai">

                        </a-table>
                    </section>
                </a-tab-pane>
                <a-tab-pane key="2" tab="生产建议">
                    <!--按钮-->
                    <!-- <div class="btn-wrapper clearfix">
                        <a-row class="text-center clearfix">
                            <a-col :xs="24" :sm="24" :md="24" :lg="16" :xl="12">
                                <a-col :xs="btnGrid.xs" :sm="btnGrid.sm" :md="btnGrid.md" :lg="btnGrid.lg" :xl="btnGrid.xl">
                                    <a-dropdown class="btns" key="sheng1" style="display:block">
                                        <a class="ant-dropdown-link" @click="e => e.preventDefault()">
                                            <a-icon class="btns-icon" type="snippets" />
                                            <div class="btns-text">生单
                                                <a-icon type="down" />
                                            </div>
                                        </a>
                                        <a-menu slot="overlay">
                                            <a-menu-item key="0">
                                                <a href="javascript:void(0)" @click="generateProduction(1)">生成生产订单</a>
                                            </a-menu-item>
                                        </a-menu>
                                    </a-dropdown>
                                </a-col>
                            </a-col>
                        </a-row>
                    </div> -->

                    <section class="work-plan-content advice-warpper">
                        <a-table :columns="columns" :data-source="productionData" :pagination="false" bordered :row-selection="rowSelectionSen">

                        </a-table>
                    </section>
                </a-tab-pane>
            </a-tabs>
        </div>
    </div>
</template>
<script>
import { getAction } from "@/api/manage";
import { save, list, remove } from "@/api/production/productionMRP";
import { mapState } from "vuex";
import moment from "moment";
export default {
    name: 'productionAdvice',
    props: ['userName', 'age'],
    data () {
        return {
            //----------↑样式----------
            btnGrid: {
                xs: 2,
                sm: 2,
                md: 2,
                lg: 2,
                xl: 2
            },
            grid: {
                xs: 24,
                sm: 24,
                md: 12,
                lg: 8,
                xl: 8,
                xxl: 6
            },
            formLayout: "inline",
            fromStyle: { paddingTop: "8px" },
            labelCol: { span: 10 },
            wrapperCol: { span: 14 },
            editFalse: {
                borderBottom: '1px solid #ded7d7'
            },
            //----------↑样式----------
            columns: [{
                title: '序号',
                dataIndex: '',
                width: 50,
                customRender: (text, record, index) => {
                    return <span>{index + 1}</span>
                },
                align: 'center'
            }, {
                title: 'MRP运算单号',
                dataIndex: 'mrpCode',
            }, {
                title: '物料编码',
                dataIndex: 'inventoryCode',
            }, {
                title: '物料名称',
                dataIndex: 'inventoryName',
            }, {
                title: '规格型号',
                dataIndex: 'inventorySpecification',
            }, 
            // {
            //     title: '客户',
            //     dataIndex: 'crmName',
            //     width: 140,
            // },
            {
                title: '子件BOM',
                dataIndex: 'bomVersionCode',
                width: 80,
            }, {
                title: '计量单位',
                dataIndex: 'unitName',
            }, {
                title: '数量',
                dataIndex: 'baseQuantity',
            }, {
                title: '建议量',
                dataIndex: 'suggestAmount',
                width: 120,
            }, {
                title: '累积下达数量',
                dataIndex: 'releasedQuantity',
                width: 120,
            }, {
                title: '需求日期',
                dataIndex: 'needDate',
                width: 120,
            }],
            procurementData: [],//采购
            productionData: [],//生产

            selectedRowKeysCai: [],
            selectedRowKeysSen: [],

            selectedDataCai: [],//采购
            selectedDataSen: [],//生产
            activeKey: '1',
        }
    },
    watch: {

    },
    activated () {
        this.procurementData = [];//采购
        this.productionData = [];//生产
        if (this.mrpData.length > 0) {
            this.mrpData.forEach(item => {
                if (item.type == 1) {
                    item.needDate = item.planBeginDate
                    this.procurementData.push(item);
                } else if (item.type == 2) {
                    item.needDate = item.planEndDate
                    this.productionData.push(item);
                }
            });
        } else {
            // this.$confirm({
            //     icon: () => { return <a-icon type="exclamation-circle" /> },
            //     title: '提示',
            //     content: '库存数量充足，无需采购和生产',
            //     okText: '确定',
            //     cancelText: '关闭',
            //     onOk() { },
            //     onCancel() { },
            // });
        }
    },
    computed: {
        ...mapState({
            mrpData: state => state.order.mrpBackData,
        }),
        rowSelectionCai () {
            //采购计划
            return {
                columnWidth: 26,
                selectedRowKeys: this.selectedRowKeysCai,
                onChange: this.onSelectChangeCai
            }
        },
        rowSelectionSen () {
            //生产计划
            return {
                columnWidth: 26,
                selectedRowKeys: this.selectedRowKeysSen,
                onChange: this.onSelectChangeSen
            }
        }
    },
    created () {
        // this.procurementData = [];//采购
        // this.productionData = [];//生产
    },
    methods: {
        moment,
        callback (key) {
            //tabs 切换事件
            this.activeKey = key + '';
        },
        generateBills (t) {
            //采购生单
            if (this.selectedDataCai.length < 1) {
                this.$message.warning('未选择采购建议数据')
                return;
            }
           
            this.selectedDataCai.forEach(item => {
                item.baseUnit = item.unitId;
                item.baseUnitName = item.unitName;
                item.baseUnit_temp_id = item.unitId;
                //  //如果是 多计量
                // if (item.unitGroupId) {
                //     item.unitId= null;
                //     item.unitName = null;
                // }
                item.baseQuantity = item.suggestAmount;
                item.mrpResultId = item.id;
                item.mrpCode = item.mrpCode;
            });
            console.log("需求建议：",this.selectedDataCai);
            let array = {
                voucherCode: '',
                detailList: this.selectedDataCai,
                sourceVoucherCode: this.selectedDataCai[0].mrpCode,//来源单号
                sourceVoucherType: 'MrpRecord',//来源单据
                sourceVoucherDate: '',//来源日期
                voucherDate: moment().format('YYYY-MM-DD'),//单据日期
                mrpCode: this.selectedDataCai[0].mrpCode,

            };

            if (t == 1) {
                array.generateSourceCode = 'PurchaseRequisition';//生单标致(请购单)
            } else if (t == 2) {
                array.generateSourceCode = 'PurchaseOrder';//生单标致(采购订单)
            }
            console.log("----需求建议 生单组装数据--------：",array);
            this.$store.commit('SET_ARR', array);

            if (t == 1) {
                //生成请购单
                this.selectedRowKeysCai = [];
                this.selectedDataCai = [];

                setTimeout(() => {
                    this.$router.push({ path: '/procurement/purchaseRequisition' })
                }, 500)
            }

            if (t == 2) {
                this.selectedRowKeysCai = [];
                this.selectedDataCai = [];
                //生成采购订单
                setTimeout(() => {
                    this.$router.push({ path: '/procurement/purchaseOrder' })
                }, 500)
            }
        },
        generateProduction (t) {
            //生产生单
            if (this.selectedDataSen.length < 1) {
                this.$message.warning('未选择生产建议数据')
                return;
            }
            this.selectedDataSen.forEach(item => {
                item.baseUnit = item.unitId;
                item.baseUnitName = item.unitName;
                item.baseUnit_temp_id = item.unitId;
             
                item.baseQuantity = item.suggestAmount;
                item.mrpResultId = item.id;
                item.mrpCode = item.mrpCode;
                item.planBeginDate = item.planBeginDate
                item.planEndDate = item.needDate
            });
            let array = {
                detailList: this.selectedDataSen,
                sourceVoucherCode: this.selectedDataSen[0].mrpCode,//来源单号
                sourceVoucherType: 'MrpRecord',//来源单据 
                sourceVoucherDate: '',//来源日期
                voucherDate: moment().format('YYYY-MM-DD'),//单据日期
                mrpCode: this.selectedDataSen[0].mrpCode,

                generateSourceCode: 'ProductionOrder'//生单标致（生单订单）
            };
            this.$store.commit('SET_ARR', array);

            if (t == 1) {
                this.selectedRowKeysSen = [];
                this.selectedDataSen = [];
                //生产需求计划
                setTimeout(() => {
                    this.$router.push({ path: '/production/management/productionOrder' })
                }, 500)
            }

            if (t == 2) {
                this.selectedRowKeysSen = [];
                this.selectedDataSen = [];
                //生成生产加工单
                setTimeout(() => {
                    this.$router.push({ path: '/production/productionWork' })
                }, 500)
            }
        },
        onSelectChangeCai (keys, rows) {
            console.log(keys, rows)
            //采购
            this.selectedRowKeysCai = keys;
            this.selectedDataCai = rows;
        },
        onSelectChangeSen (keys, rows) {
            //生产
            this.selectedRowKeysSen = keys;
            this.selectedDataSen = rows;
        }
    }
}
</script>
<style lang="less">
    @import '~@/style/purchase/voucher.less';
    .mrp-result-box {
        .ant-tabs-bar {
            margin-bottom: 0px;
            border-bottom: 1px solid #ddd;
        }
    }
    .advice-warpper {
        margin-top: 10px;
        .ant-table-placeholder {
            position: relative !important;
            top: 0;
        }
    }
</style>